package app

import (
	"time"
)

type AppErr int

const (
	ErrAnalysisDataGetFailed	AppErr 	= 103
	ErrLikedCoinNotExists		AppErr 	= 102
	ErrLikedCoinExists			AppErr 	= 101
	ErrNone 					AppErr 	= 0
	ErrParameter 				AppErr 	= -1001
	ErrGetDataFailed 			AppErr 	= -1002
	ErrCoinNotExists 			AppErr 	= -1100
	ErrSymbolNotExists 			AppErr  = -1101
	ErrExchangeNotExists 		AppErr  = -1200
	ErrExchangeNotEnable 		AppErr  = -1201
	ErrTickerFailed 			AppErr  = -1300
	ErrDepthFailed 				AppErr  = -1400
	Err404 						AppErr  = -1404
	ErrInnerError 				AppErr 	= -1500
	ErrUnknownError 			AppErr 	= -9999
)

func (e AppErr) ShowMessage() string {
	errMap := map[AppErr]string{
		103: 			"failed get analysis data",
		102: 			"liked coin not exists",
		101: 			"liked coin exists",
		0: 				"",
		-1404: 			"Page not exists or method not support",
		-1001: 			"Parameter error",
		-1002: 			"Get data failed",

		-1100: 			"Coin not exists",			// 1100 开头的与币种有关
		-1101: 			"Symbol not exists",

		-1200: 			"Exchange not exists",		// 1200 开头的与交易所相关
		-1201: 			"Exchange not enable",

		-1300: 			"Get ticker info failed",
		-1400: 			"Get depth info failed",

		-1500: 			"Inner error",

		-9999: 			"Unknown error",
	}

	if msg, ok := errMap[e]; ok {
		return msg
	}
	return "Unknown error"
}

func AppMsg(m interface{}) interface{} {
	type errorResponse struct {
		AppBaseResponse
		Data 		interface{}			`json:"data"`
	}

	var resp errorResponse
	resp.Timestamp = time.Now().Unix()
	resp.ErrorCode = ErrNone
	resp.ErrorMessage = ErrNone.ShowMessage()
	resp.Data = m
	return resp
}

func AppErrorMsg(code AppErr) interface{} {
	type errorResponse struct {
		AppBaseResponse
		Data 		interface{}			`json:"data"`
	}

	var resp errorResponse
	resp.Timestamp = time.Now().Unix()
	resp.ErrorCode = code
	resp.ErrorMessage = code.ShowMessage()
	resp.Data = nil
	return resp
}